package com.zeroes.cons;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.zeroes.common_prelude.CGSize;
import org.zeroes.common_prelude.Quad;
import org.zeroes.common_prelude.Rect;

import android.graphics.Color;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;

public class ZSColoredQuad extends ZSSprite{

	private int red;
	private int green;
	private int blue;
	public ZSColoredQuad(String tag, int color, CGSize size){
		
	super();

	this.tag=tag;
	this.red=Color.red(color);
	this.green=Color.green(color);
	this.blue=Color.blue(color);
	
	init(size);
	}
	
	public ZSColoredQuad(String tag, int[] color,CGSize size){
		
	super();

	this.tag=tag;
	this.red=color[0];
	this.green=color[1];
	this.blue=color[2];
	init(size);
	}
	
	private void init(CGSize sz){
		
    	Rect r = new Rect(0,0,sz.width,sz.height);
    	rect =r;
    	quad=new Quad();
    	quad.set_from_rect(r);
    	_texture = new ZSTexture2D(sz);
    	float[] mVertsData1 =
            { 
    			0.0f, 0.0f, 0.0f, // Position 0
                    0.0f, 0.0f,0.0f, 0.0f, // TexCoord 0
                    -0.0f, 0.0f, 0.0f, // Position 1
                    0.0f, 1.0f,0.0f, 0.0f, // TexCoord 1
                    0.0f, 0.0f, 0.0f, // Position 2
                    1.0f, 1.0f,0.0f, 0.0f, // TexCoord 2
                    0.0f, 0.0f, 0.0f, // Position 3
                    1.0f, 0.0f,0.0f, 0.0f, // TexCoord 3
            };
        
        _verts = ByteBuffer.allocateDirect(mVertsData1.length * 4)
                .order(ByteOrder.nativeOrder()).asFloatBuffer();
        _verts.put(mVertsData1).position(0);
        
        
        short[] mIndicesData =
            { 
                    0, 1, 2, 0, 2, 3 
            };
        
        indices = ByteBuffer.allocateDirect(mIndicesData.length * 2)
                .order(ByteOrder.nativeOrder()).asShortBuffer();
        indices.put(mIndicesData).position(0);
        Log.v("gtrans","SUCCES INIT colored quad");
	}
	@Override
	protected void updateVertsfromQuad(Quad q){
		 Log.v("gtrans","START UPDATE colored quad");
    	int w=_gl.mWidth,h=_gl.mHeight;
    	float hw=w/2,hh=h/2;
    

		float[] mVertsData1 =
            { 
                    -hw+q.tlx, hh-q.tly, 0.0f, // Position 0
                    red/255.0f, green/255.0f,blue/255.0f,1.0f, // Color 0
                    -hw+q.blx, hh-q.bly, 0.0f, // Position 1
                    red/255.0f, green/255.0f,blue/255.0f,1.0f, // Color 1
                    -hw+q.brx,hh-q.bry, 0.0f, // Position 2
                    red/255.0f, green/255.0f,blue/255.0f,1.0f, // Color 2
                    -hw+q.trx, hh-q.try_, 0.0f, // Position 3
                    red/255.0f, green/255.0f,blue/255.0f,1.0f, // Color 3
            };
        _verts.position(0);
        _verts.put(mVertsData1).position(0);
    	

        
	}
	
	public void changeColor(int newcolor){
		this.red=Color.red(newcolor);
		this.green=Color.green(newcolor);
		this.blue=Color.blue(newcolor);
		this.marktransformDirty();
		
	}
	
	public void changeColor(int[] newcolor){
		this.red=newcolor[0];
		this.green=newcolor[1];
		this.blue=newcolor[2];
		this.marktransformDirty();
		
	}
	
	@Override
	protected void draw(){
		Log.v("gtrans","DRAW colored quad");
		if(!visible||ouv)
		 return;
		
		int prog = _gl.mColorProgramObject;
		GLES20.glUseProgram(prog);
		
		 int PositionLoc = GLES20.glGetAttribLocation(prog, "aPosition");
	     int colLoc = GLES20.glGetAttribLocation(prog, "aColor" );

	     
	        _verts.position(0);	     
	        GLES20.glVertexAttribPointer ( PositionLoc, 3, GLES20.GL_FLOAT, 
                    false, 
                    7 * 4, _verts );
	        _verts.position(3);
	        GLES20.glVertexAttribPointer ( colLoc, 4, GLES20.GL_FLOAT,
                    false, 
                    7 * 4, 
                    _verts );

	        GLES20.glEnableVertexAttribArray ( PositionLoc );
	        GLES20.glEnableVertexAttribArray ( colLoc );
	        


	        // Set the base map sampler to texture unit to 0

	        GLES20.glUniform1f(GLES20.glGetUniformLocation(prog, "opacity"), opacity);
	        //Matrix.multiplyMM(MVPMatrix, 0, _gl.mPMatrix.data, 0, MVMatrix, 0);
	        
	        Matrix.multiplyMM(MVPMatrix, 0, _gl.matP.data, 0, _gl.matMV.data, 0);
	        GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(prog, "matP"), 1, false, _gl.matP.data, 0);
	        GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(prog, "matMV"), 1, false, _gl.matMV.data, 0);
	        //GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(prog, "uMVPMatrix"), 1, false, MVPMatrix, 0);
	        GLES20.glDrawElements ( GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, indices );    
	     /* if(this.tag.equals("text"))
	          Log.v("gtrans", "draw from sprite text .... ");*/

	}
}
